package com.xiaomei.manager.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiaomei.manager.dao.BroadcastMapper;
import com.xiaomei.manager.dao.BroadcastRecordMapper;
import com.xiaomei.manager.model.Broadcast;
import com.xiaomei.manager.model.BroadcastRecord;
import com.xiaomei.manager.service.IBroadcastService;
import com.xiaomei.manager.utils.Constants;
import com.xiaomei.manager.web.dto.req.DayCommentDto;
import com.xiaomei.manager.web.dto.req.DayPostDto;
import com.xiaomei.manager.web.dto.req.ProbabilityDto;
import com.xiaomei.manager.web.page.Page;

@Service
public class BroadcastServiceImpl implements IBroadcastService {

	@Autowired
	private BroadcastMapper broadcastMapper;
	
	@Autowired
	private BroadcastRecordMapper broadcastRecordMapper;
	
	@Override
	public List<Broadcast> selectBroadcastForPage(Page<Broadcast> page) {
		// TODO Auto-generated method stub
		return broadcastMapper.selectBroadcastForPage(page);
	}

	public boolean addBroadcast(Broadcast broadcast) {
		return broadcastMapper.insertSelective(broadcast) == 1 ? true : false;
	}

	@Override
	public Broadcast getBroadcastById(Integer id) {
		// TODO Auto-generated method stub
		return (Broadcast) broadcastMapper.selectByPrimaryKey(id);
	}

	@Override
	public boolean modifyBroadcastById(Broadcast broadcast) {
		// TODO Auto-generated method stub
		return broadcastMapper.updateByPrimaryKeySelective(broadcast) == 1 ? true : false;
	}

	@Override
	public List<BroadcastRecord> selectByBroadcastId(Integer broadcastId) {
		// TODO Auto-generated method stub
		return broadcastRecordMapper.selectByBroadcastId(broadcastId);
	}
	

	@Override
	public int insertBroadcastRecord(BroadcastRecord record) {
		// TODO Auto-generated method stub
		return broadcastRecordMapper.insertSelective(record);
	}

	@Override
	public boolean disableBroadcastById(Integer id) {
		// TODO Auto-generated method stub
		Broadcast broadcast = new Broadcast();
		broadcast.setId(id);
		broadcast.setState(Constants.BROADCAS_STATE_1);
		
		return broadcastMapper.updateByPrimaryKeySelective(broadcast) == 1 ? true : false;
	}

	@Override
	public boolean disableBroadcastRecordById(Integer broadcastId, Integer id) {
		// TODO Auto-generated method stub
		BroadcastRecord record = new BroadcastRecord();
		record.setId(id);
		record.setState(Constants.BROADCAS_STATE_1);
		
		boolean result = broadcastRecordMapper.updateByPrimaryKeySelective(record) == 1 ? true : false;
		if(result) {
			result = broadcastMapper.updateCommentCountById(broadcastId) == 1 ? true : false;
		}
		if(result) {
			return true;
		}
		throw new RuntimeException("更新评论数失败");
	}

	@Override
	public int pushBeforeBroadcast() {
		// TODO Auto-generated method stub
		return broadcastMapper.pushBeforeBroadcast();
	}
	/**
	 * 日发广播量
	 * @param page
	 * @return
	 */
	@Override
	public List<DayPostDto> getDayPost(Page<DayPostDto> page) {
		// TODO Auto-generated method stub
		return broadcastMapper.selectDayPost(page);
	}
	/**
	 * 日评论量
	 * @param page
	 * @return
	 */
	@Override
	public List<DayCommentDto> getDayComment(Page<DayCommentDto> page) {
		// TODO Auto-generated method stub
		return broadcastMapper.selectDayComment(page);
	}
	/**
	 * 日评点赞量
	 * @param page
	 * @return
	 */
	@Override
	public List<DayPostDto> getDayFabulous(Page<DayPostDto> page) {
		// TODO Auto-generated method stub
		return broadcastMapper.selectDayFabulous(page);
	}
	/**
	 * 评论点赞概率
	 */
	@Override
	public List<ProbabilityDto> getProbability(Page<ProbabilityDto> page) {
		// TODO Auto-generated method stub
		return broadcastMapper.selectProbability(page);
	}

	@Override
	public int updateCommentCount(Integer id) {
		// TODO Auto-generated method stub
		return broadcastMapper.updateCommentCount(id);
	}
}
